package com.boesch.electroOffice.presentation.staff;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import org.springframework.stereotype.Controller;

import com.boesch.electroOffice.model.enums.SessionParameter;
import com.boesch.electroOffice.model.enums.SubfunctionIdentifier;
import com.boesch.electroOffice.utils.ConfigurationUtils;
import com.boesch.electroOffice.utils.PortletUtils;
import com.boesch.electroOffice.utils.RoleUtils;
import com.liferay.portal.model.Role;

/**
 * This class is used for the navigation in staff portlet
 * 
 * @author Ralph Koster
 *
 */
@ManagedBean
@SessionScoped
@Controller
public class StaffNavigationController implements Serializable {

	private static final long serialVersionUID = 5704924564605911683L;
	
	private String navigationPath = "/jsf/staff/week_report/week_report.xhtml";

	public String getNavigationPath() {
		return navigationPath;
	}
	
	public void setNavigationPath(ActionEvent event) {
		String key = (String)event.getComponent().getAttributes().get("key");
		if(key.equals(SubfunctionIdentifier.CURRENT_WEEK_REPORT.getValue())) {
			navigationPath = "/jsf/staff/week_report/week_report.xhtml";
			PortletUtils.setSessionAttribute(SessionParameter.SUBFUNCTION_KEY.getValue(), key);
			PortletUtils.setSessionAttribute(SessionParameter.RESET.getValue(), "true");
		} else if(key.equals(SubfunctionIdentifier.HOLIDAY_CREDIT.getValue())) {
			navigationPath = "/jsf/staff/holiday_credit/holiday_credit.xhtml";
		} else if(key.equals(SubfunctionIdentifier.OVERTIME.getValue())) {
			navigationPath = "/jsf/staff/overtime/overtime.xhtml";
		} else if(key.equals(SubfunctionIdentifier.ACHIEVEMENT_CURRENT_MONTH.getValue())) {
			PortletUtils.setSessionAttribute(SessionParameter.SUBFUNCTION_KEY.getValue(), key);
			navigationPath = "/jsf/common/pdf_reader.xhtml";
		} else if(key.equals(SubfunctionIdentifier.ACHIEVEMENT_LAST_MONTH.getValue())) {
			PortletUtils.setSessionAttribute(SessionParameter.SUBFUNCTION_KEY.getValue(), key);
			navigationPath = "/jsf/common/pdf_reader.xhtml";
		} else if(key.equals(SubfunctionIdentifier.ACHIEVEMENT_CURRENT_YEAR.getValue())) {
			PortletUtils.setSessionAttribute(SessionParameter.SUBFUNCTION_KEY.getValue(), key);
			navigationPath = "/jsf/common/pdf_reader.xhtml";
		} else if(key.equals(SubfunctionIdentifier.SALARY_LIST_STAFF.getValue())) {
			PortletUtils.setSessionAttribute(SessionParameter.SUBFUNCTION_KEY.getValue(), key);
			navigationPath = "/jsf/common/pdf_reader.xhtml";
		} else {
			navigationPath = "/jsf/staff/week_report/week_report.xhtml";
		}
	}
	
	public boolean hasPermission(String key) {
		boolean returnValue = false;
		List<Role> roles = RoleUtils.getCurrentUsersRoles();
		for(Role role : roles) {
			if(ConfigurationUtils.getRoleSubfunctionPreference(role.getName(), key)) {
				returnValue = true;
				break;
			}
		}
		return returnValue;
	}
	
	public String[] getFirstLinkKeys() {
		return StaffSubfunctionUtils.getFirstLinkKeys();
	}
	
	public String[] getMainLinkKeys() {
		return StaffSubfunctionUtils.getMainLinkKeys();
	}
	
	public String[] getReportLinkKeys() {
		return StaffSubfunctionUtils.getReportLinkKeys();
	}
}
